#include"c3pom/portal.h"
#include<sys/time.h>
#include<iostream>
using namespace c3pom;
using namespace std;
static long pbnum=0;
Portal::Portal()
{
	portalBuf=NULL;
}
int Portal::dataLength()
{
	int length=-1;
	if(portalBuf!=NULL)
		length=portalBuf->dataLength();
	return length;
}
void Portal::operator<<(PortalBlock* pblock)
{
	if(portalBuf!=NULL)
		portalBuf->push(pblock);
}

void Portal::operator>>(PortalBlock* &pblock)
{
	if(portalBuf!=NULL)
		pblock=portalBuf->pop();
}
int PortalBuf::dataLength()
{
	return -1;
}

PortalBlock::PortalBlock(Object data,std::string owner,Portal* resultPortal)
{
	omp_init_lock(&lock);
	this->owner=owner;
	this->data=data;
	this->resultPortal=resultPortal;
	//生成时间戳
	timeval tv;
	timeStamp+=gettimeofday(&tv,NULL);//第一部分，生成时间
	timeStamp+=pbnum++;//第二部分，当成生成的数量--这个需要使用原子操作
	timeStamp+=(long)this;//第三部分，内存地址
}

string PortalBlock::getStatu()
{
	omp_set_lock(&lock);
	string s=this->statu;
	omp_unset_lock(&lock);
	return s;
}

int PortalBlock::setStatu(string statu)
{
	omp_set_lock(&lock);
	this->statu=statu;
	omp_unset_lock(&lock);
	return 0;
}

Object PortalBlock::getResult()
{
	omp_set_lock(&lock);
	Object o=this->result;
	omp_unset_lock(&lock);
	return o;
}

int PortalBlock::setResult(Object result)
{
	omp_set_lock(&lock);
	this->result=result;
	omp_unset_lock(&lock);
	return 0;
}

Portal* PortalBlock::getResultPortal()
{
	omp_set_lock(&lock);
	Portal* portal=this->resultPortal;
	omp_unset_lock(&lock);
	return portal;
}

string PortalBlock::getTimeStamp()
{
	omp_set_lock(&lock);
	string s=this->timeStamp;
	omp_unset_lock(&lock);
	return s;
}

string PortalBlock::getOwner()
{
	omp_set_lock(&lock);
	string s=this->owner;
	omp_unset_lock(&lock);
	return s;
}

Object PortalBlock::getData()
{
	return data;
}


